﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using System.Data.SqlClient;
using System.Collections;
using DAL.DataOperate;

namespace BLL.Business
{
    public class OrderBLL
    {
        DAL.DataOperate.OrderDAL orderDAL = new DAL.DataOperate.OrderDAL();

        #region 订单检索
        public DataSet SelectAll()
        {
            return orderDAL.SelectAll();
        }
        public DataSet SelectByID(int OrderId)
        {
            if (OrderId.ToString() == "" || OrderId.ToString() == null)
            {
                return null;
            }
            else
            {
                return orderDAL.SelectByID(OrderId);
            }
        }
        public DataSet SelectByUserName(String UserName)
        {
            if (UserName == "" || UserName == null)
                return null;
            else
                return orderDAL.SelectByUserName(UserName);
        }
        public DataSet SelectDetail(int DetailId)
        {
            if (DetailId.ToString() == "")
            {
                return null;
            }
            else
            {
                return orderDAL.SelectDetail(DetailId);
            }
        }
        #endregion

        #region 操作订单
        public bool InsertOrder(Model.Order order,ref int InsertedorderId)
        {
            if ((order.User_Idint.ToString() == "" || order.User_Idint.ToString() == null) ||
               (order.Ord_ContactManstr.ToString() == "" || order.Ord_ContactManstr.ToString() == null) ||
               (order.Ord_Telephonestr.ToString() == "" || order.Ord_Telephonestr.ToString() == null) ||
               (order.Ord_PostalCodestr.ToString() == "" || order.Ord_PostalCodestr.ToString() == null) ||
               (order.Ord_Addressstr.ToString() == "" || order.Ord_Addressstr.ToString() == null) ||
               (order.Ord_Statusint.ToString() == "" || order.Ord_Statusint.ToString() == null) ||
               (order.Ord_IsCheckedint.ToString() == "" || order.Ord_IsCheckedint.ToString() == null) ||
               (order.Ord_Deliverystr.ToString() == "" || order.Ord_Deliverystr.ToString() == null) ||
               (order.Ord_Paymentstr.ToString() == "" || order.Ord_Paymentstr.ToString() == null) ||
               (order.Ord_Remarkstr.ToString() == "" || order.Ord_Remarkstr.ToString() == null) ||
               (order.Ord_Datedate.ToString() == "" || order.Ord_Datedate.ToString() == null))
            {
                return false;
            }
            else
            {
               return  orderDAL.InsertOrder(order,ref InsertedorderId);
            }
        }
        public bool UpdateOrderStatus(int OrderId, int newStatus)      // 订单状态（用户）
        {
            if ((OrderId.ToString() == "" || OrderId.ToString() == null) ||
                (newStatus.ToString() == "" || newStatus.ToString() == null))
            {
                return false;
            }
            else
            {
                return orderDAL.UpdateOrderStatus(OrderId, newStatus);
            }
        }
        public bool UpdateOrderChecked(int OrderId, int newChecked)     // 订单状态（管理员）
        {
            if ((OrderId.ToString() == "" || OrderId.ToString() == null) ||
                (newChecked.ToString() == "" || newChecked.ToString() == null))
            {
                return false;
            }
            else
            {
                return orderDAL.UpdateOrderChecked(OrderId, newChecked);
            }
        }
        public bool DeleteOrder(int OrderId)
        {
            if (OrderId.ToString() == "" || OrderId.ToString() == null)
            {
                return false;
            }
            else
            {
                return orderDAL.DeleteOrder(OrderId);
            }
        }
        #endregion

        #region 操作订单详情
        public bool InsertOrderDetail(Model.Order order)
        {
            if ((order.Ord_Idint.ToString() == "" || order.Ord_Idint.ToString() == null) ||
                (order.Book_Idint.ToString() == "" || order.Book_Idint.ToString() == null) ||
                (order.Detail_Quantityint.ToString() == "" || order.Detail_Quantityint.ToString() == null) ||
                (order.Detail_TotalCostflo.ToString() == "" || order.Detail_TotalCostflo.ToString() == null))
            {
                return false;
            }
            else
            {
                return orderDAL.InsertOrderDetail(order);
            }
        }
        public bool UpdatreOrderDetail(int Detail_Idint, int Detail_IsCommentbit)
        {
            if (Detail_Idint.ToString() == "" || Detail_IsCommentbit.ToString() == "")
            {
                return false;
            }
            else
            {
                return orderDAL.UpdatreOrderDetail(Detail_Idint, Detail_IsCommentbit);
            }
        }
        #endregion

        #region  订单添加事物
        public bool InsertOrderTransaction(Model.Order order, DataTable productTable)
        {
            if ((order.User_Idint.ToString() == "" || order.User_Idint.ToString() == null) ||
               (order.Ord_ContactManstr.ToString() == "" || order.Ord_ContactManstr.ToString() == null) ||
               (order.Ord_Telephonestr.ToString() == "" || order.Ord_Telephonestr.ToString() == null) ||
               (order.Ord_PostalCodestr.ToString() == "" || order.Ord_PostalCodestr.ToString() == null) ||
               (order.Ord_Addressstr.ToString() == "" || order.Ord_Addressstr.ToString() == null) ||
               (order.Ord_Statusint.ToString() == "" || order.Ord_Statusint.ToString() == null) ||
               (order.Ord_IsCheckedint.ToString() == "" || order.Ord_IsCheckedint.ToString() == null) ||
               (order.Ord_Deliverystr.ToString() == "" || order.Ord_Deliverystr.ToString() == null) ||
               (order.Ord_Paymentstr.ToString() == "" || order.Ord_Paymentstr.ToString() == null) ||
               (order.Ord_Datedate.ToString() == "" || order.Ord_Datedate.ToString() == null) ||
               (productTable == null) ||
               (productTable.Rows.Count == 0))
            {
                return false;
            }
            else
            {
                productTable.Columns.Add("OwnerId");  // 添加图书的主人编号
                foreach (DataRow dr in productTable.Rows)
                {
                    int UserId = orderDAL.GetSellerId(Int32.Parse( dr["Book_Idint"].ToString()));
                    dr["OwnerId"] = UserId.ToString();
                }

                DataTable tempTable = new DataTable();
                tempTable.Columns.Add("Book_Idint");
                tempTable.Columns.Add("Book_Namestr");  // 书名
                tempTable.Columns.Add("Book_Price");    // 价格
                tempTable.Columns.Add("Book_Count");    // 数量
                tempTable.Columns.Add("OwnerId");       // 数量
                Hashtable ownerHash = new Hashtable();  // 保存卖书的用户名
                for (int i = 0; i < productTable.Rows.Count; i++)
                {
                    if (!ownerHash.Contains(productTable.Rows[i]["OwnerId"].ToString()))
                    {
                        ownerHash.Add(productTable.Rows[i]["OwnerId"].ToString(), "");
                        tempTable.Clear();
                        for (int j = 0; j < productTable.Rows.Count; j++)
                        {
                            if (productTable.Rows[i]["OwnerId"].ToString() == productTable.Rows[j]["OwnerId"].ToString())
                            {
                                tempTable.ImportRow(productTable.Rows[j]);
                            }
                        }
                        if (!orderDAL.InsertOrderTransaction(order, tempTable))
                        {
                            return false;
                        }
                    }
                }
                return true;
            }
        }
        #endregion

        #region 二手操作区
        public DataSet SelectSecondOrderByUserId(int UserId)
        {
            if (UserId.ToString() == "")
            {
                return null;
            }
            else
            {
                return orderDAL.SelectSecondOrderByUserId(UserId);
            }
        }
        #endregion   
    }
}
